package com.ruoyi.recruit.controller;

import java.util.List;

import com.ruoyi.api.system.utils.ShiroUtils;
import com.ruoyi.api.system.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.recruit.domain.RecruitContent;
import com.ruoyi.recruit.domain.RecruitInfo;
import com.ruoyi.recruit.domain.RecruitUser;
import com.ruoyi.recruit.service.IRecruitContentService;
import com.ruoyi.recruit.service.IRecruitInfoService;
import com.ruoyi.recruit.service.IRecruitUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.recruit.domain.RecruitUserInterview;
import com.ruoyi.recruit.service.IRecruitUserInterviewService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 面试人员成绩Controller
 * 
 * @author ruoyi
 * @date 2022-05-05
 */
@Controller
@RequestMapping("/recruit/interview")
public class RecruitUserInterviewController extends BaseController
{
    private String prefix = "recruit/interview";

    @Autowired
    private IRecruitUserInterviewService recruitUserInterviewService;

    @Autowired
    private IRecruitInfoService recruitInfoService;

    @Autowired
    private IRecruitContentService recruitContentService;

    @Autowired
    private IRecruitUserService recruitUserService;

    @RequiresPermissions("recruit:interview:view")
    @GetMapping(value = {"", "/{recruitId}"})
    public String interview(@PathVariable(value = "recruitId", required = false) Long recruitId, ModelMap mmap)
    {
        RecruitInfo recruitInfo = new RecruitInfo();
        if(StringUtils.isNotNull(recruitId)){
            recruitInfo = recruitInfoService.selectRecruitInfoByRecruitId(recruitId);
        }
        mmap.put("recruitInfo", recruitInfo);
        mmap.put("infos", recruitInfoService.selectRecruitInfoList(new RecruitInfo()));
        return prefix + "/interview";
    }

    /**
     * 查询面试人员成绩列表
     */
    @RequiresPermissions("recruit:interview:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RecruitUserInterview recruitUserInterview)
    {
        startPage();
        List<RecruitUserInterview> list = recruitUserInterviewService.selectRecruitUserInterviewList(recruitUserInterview);
        return getDataTable(list);
    }

    /**
     * 导出面试人员成绩列表
     */
    @RequiresPermissions("recruit:interview:export")
    @Log(title = "面试人员成绩", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RecruitUserInterview recruitUserInterview)
    {
        List<RecruitUserInterview> list = recruitUserInterviewService.selectRecruitUserInterviewList(recruitUserInterview);
        ExcelUtil<RecruitUserInterview> util = new ExcelUtil<RecruitUserInterview>(RecruitUserInterview.class);
        return util.exportExcel(list, "面试人员成绩数据");
    }

    /**
     * 导入面试人员
     * @param file
     * @param recruitId
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @Log(title = "面试人员", businessType = BusinessType.IMPORT)
    @RequiresPermissions("recruit:interview:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, Long recruitId, boolean updateSupport) throws Exception
    {
        ExcelUtil<RecruitUserInterview> util = new ExcelUtil<RecruitUserInterview>(RecruitUserInterview.class);
        List<RecruitUserInterview> userInterviewList = util.importExcel(file.getInputStream());
        String message = recruitUserInterviewService.importUserInterview(userInterviewList, recruitId, updateSupport, ShiroUtils.getLoginName());
        return AjaxResult.success(message);
    }

    @RequiresPermissions("recruit:interview:import")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<RecruitUserInterview> util = new ExcelUtil<RecruitUserInterview>(RecruitUserInterview.class);
        return util.importTemplateExcel("面试人员");
    }

    /**
     * 新增面试人员成绩
     */
    @GetMapping("/add/{recruitId}")
    public String add(@PathVariable("recruitId") Long recruitId, ModelMap mmap)
    {
        RecruitInfo recruitInfo = new RecruitInfo();
        if(recruitId != 0){
            recruitInfo = recruitInfoService.selectRecruitInfoByRecruitId(recruitId);
        }
        mmap.put("recruitInfo", recruitInfo);
        mmap.put("infos", recruitInfoService.selectRecruitInfoList(new RecruitInfo()));
        return prefix + "/add";
    }

    /**
     * 新增保存面试人员成绩
     */
    @RequiresPermissions("recruit:interview:add")
    @Log(title = "面试人员成绩", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RecruitUserInterview recruitUserInterview)
    {
        recruitUserInterview.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(recruitUserInterviewService.insertRecruitUserInterview(recruitUserInterview));
    }

    /**
     * 修改面试人员成绩
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        RecruitUserInterview recruitUserInterview = recruitUserInterviewService.selectRecruitUserInterviewById(id);
        mmap.put("recruitUserInterview", recruitUserInterview);
        mmap.put("infos", recruitInfoService.selectRecruitInfoList(new RecruitInfo()));
        return prefix + "/edit";
    }

    /**
     * 修改保存面试人员成绩
     */
    @RequiresPermissions("recruit:interview:edit")
    @Log(title = "面试人员成绩", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RecruitUserInterview recruitUserInterview)
    {
        recruitUserInterview.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(recruitUserInterviewService.updateRecruitUserInterview(recruitUserInterview));
    }

    /**
     * 删除面试人员成绩
     */
    @RequiresPermissions("recruit:interview:remove")
    @Log(title = "面试人员成绩", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(recruitUserInterviewService.deleteRecruitUserInterviewByIds(ids));
    }
}
